const axios = require('axios')
const md5 = require('md5')

let url = `http://api.fanyi.baidu.com/api/trans/vip/translate` //百度

async function baiduFY(wrod){
    return new Promise(async (resolve, reject) => {
        const salt = Math.random();
        let params = { 
            q: wrod,
            from: 'en',
            to: 'cht',
            appid: '20220103001045602',
            salt,
            sign: ''
        }
        params.sign = md5(`${params.appid}${params.q}${params.salt}ryDj3OHS0nca7vj3MFNG`)
        params.q = encodeURIComponent(params.q)
        axios.get(url, {
            params
        }).then(res => {
            if(res.data && res.data.trans_result){
                resolve({
                    src: res.data.trans_result[0].src,
                    res: res.data.trans_result[0].dst
                })
            }else{
                reject(res.data)
            }
        }).catch(err => {
            reject(err)
        })
    })
}

module.exports = baiduFY